import {
  manufacturer,
  brand,
  module,
  getDeviceTypeName,
  allOperationUser,
  getRegionData,
  getGroupData,
  getBranchData,
  cfgBrancBranchTreeh,
  getPlaceManagerList
} from "@/api/cache";
import {treeselect} from "@/api/system/dept";
import {handleTree, handleTreeForSelector} from "@/utils/ruoyi";
import {listDrive} from '@/api/device/drive';
import app from './app'
import {handleAESDecrypt} from "@/utils/ase";

const device = {
  state: {
    brands: [], //匹配下拉选项数据
    manufacturers: [],  //厂家下拉选项数据
    deptOptions: [],  //部门树选项数据
    deviceModules: [],  //设备模块下拉选项数据
    deviceTypeAndBomCodeOptions: [],  //设备名称+bom下拉选项数据
    operationUserOptions: [],  //运维人员下拉选项数据
    regionDataOptions: [],  //区域树选项数据
    branchDataOptions: [],  //网点树选项数据
    placeManagerOptions: [],  //设备部署下拉选项数据
    driveOptions: [],  //驱动下拉选项数据
    groupOptions: []  //分组树数据
  },

  mutations: {
    SET_BRANDS: (state, brands) => {
      state.brands = brands
    },
    SET_MANUFACTURERS: (state, manufacturers) => {
      state.manufacturers = manufacturers
    },
    SET_DEPTOPTIONS: (state, deptOptions) => {
      state.deptOptions = deptOptions
    },
    SET_DEVICEMODULE: (state, deviceModules) => {
      state.deviceModules = deviceModules
    },
    SET_DEVICETYPEANDBOMCODEOPTIONS: (state, deviceTypeAndBomCodeOptions) => {
      state.deviceTypeAndBomCodeOptions = deviceTypeAndBomCodeOptions
    },
    SET_OPERATIONUSEROPTIONS: (state, operationUserOptions) => {
      state.operationUserOptions = operationUserOptions
    },
    SET_REGIONDATAOPTIONS: (state, regionDataOptions) => {
      state.regionDataOptions = regionDataOptions;
    },
    SET_BRANCHDATAOPTIONS: (state, branchDataOptions) => {
      state.branchDataOptions = branchDataOptions
    },
    SET_PLACEMANAGEROPTIONS: (state, placeManagerOptions) => {
      state.placeManagerOptions = placeManagerOptions
    },
    SET_DRIVEOPTIONS: (state, driveOptions) => {
      state.driveOptions = driveOptions
    },
    SET_GROUPOPTIONS: (state, groupOptions) => {
      state.groupOptions = groupOptions
    },
  },

  actions: {
    // 加载品牌缓存
    LoadBrandCache({commit}) {
      return new Promise((resolve, reject) => {
        brand().then(res => {
          const brands = res.data;
          commit('SET_BRANDS', brands);
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 加载厂家缓存
    LoadManufacturersCache({commit}) {
      return new Promise((resolve, reject) => {
        manufacturer().then(res => {
          const manufacturers = res.data;
          commit('SET_MANUFACTURERS', manufacturers);
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 加载部门树缓存
    LoadDeptOptionsTree({commit}) {
      return new Promise((resolve, reject) => {
        treeselect().then(res => {
          const deptOptions = res.data;
          commit('SET_DEPTOPTIONS', deptOptions);
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 加载设备模块
    LoadDeviceModules({commit}) {
      return new Promise((resolve, reject) => {
        module().then(res => {
          commit('SET_DEVICEMODULE', res.data);
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 加载设备型号bom编码表缓存
    LoadDeviceTypeAndBomCodeOption({commit}) {
      return new Promise((resolve, reject) => {
        getDeviceTypeName().then(res => {
          const typeAndBomCode = res.rows.map(item => {
            return {
              // 将设备型号和bom编码拼接起来，确定一台机器
              label: `${item.name}-${item.bomCode}`,
              name: item.name,
              value: item.code
            }
          })
          commit('SET_DEVICETYPEANDBOMCODEOPTIONS', typeAndBomCode);
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 加载运维人员信息缓存
    LoadOperationUserOption({commit}) {
      return new Promise((resolve, reject) => {
        allOperationUser().then(res => {
          const list = res.rows.map(item => {
            return {
              value: item.operationUserCode,
              label: item.operationUserName = handleAESDecrypt(item.operationUserName)
            }
          })
          commit('SET_OPERATIONUSEROPTIONS', list);
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 加载地区数据缓存
    LoadRegionDataOptions({commit}) {
      const provinceIdConfig = app.state.provinceIdConfig;
      return new Promise((resolve, reject) => {
        getRegionData({
          parentId: provinceIdConfig
        }).then(res => {
          const list = handleTreeForSelector(res, "id")
          commit('SET_REGIONDATAOPTIONS', list);
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 加载网点数据
    LoadBranchOptionsTree({commit}) {
      return new Promise((resolve, reject) => {
        cfgBrancBranchTreeh().then(res => {
          // const list = handleTreeForSelector(res, "branchCode","parentCode");
          commit('SET_BRANCHDATAOPTIONS', res.data);
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 加载设备部署管理数据缓存
    LoadPlaceManagerOptions({commit}) {
      return new Promise((resolve, reject) => {
        getPlaceManagerList().then(res => {
          const list = res.rows.map(item => {
            return {
              value: item.deviceId,
              label: item.deviceId,
              status: item.status
            }
            //过滤掉状态不为0的数据
          }).filter(item => {
            return item.status == 0;
          })
          commit('SET_PLACEMANAGEROPTIONS', list);
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 加载运维人员信息缓存
    LoadDriveOption({commit}) {
      return new Promise((resolve, reject) => {
        listDrive({status: 0}).then(res => {
          const list = res.rows.map(item => {
            return {
              driveName: item.driveName,
              adapterName: item.adapterName,
              driveCode: item.driveCode,
            }
          })
          commit('SET_DRIVEOPTIONS', list);
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 加载分组树缓存
    LoadGroupOptionsTree({commit}) {
      return new Promise((resolve, reject) => {
        getGroupData().then(res => {
          const groupOptions = handleTree(res.rows, "id");
          commit('SET_GROUPOPTIONS', groupOptions);
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

  }
}

export default device

